/**
 * hnjz.com Inc.
 * Copyright (c) 2004-2014 All Rights Reserved.
 */
package com.hnjz.sys.roleFun;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hnjz.common.AppException;
import com.hnjz.common.JsonResultUtil;

/**
 * 权限管理的Controller
 * 
 * @author wumi
 * @version $Id: RoleFuncController.java, v 0.1 Feb 14, 2014 10:28:52 AM wumi Exp $
 */
@Controller
public class RoleFuncController {

    /**日志*/
    private static final Log log = LogFactory.getLog(RoleFuncController.class);

    @Autowired
    private RoleFuncManager  roleFuncManager;

    /**
     * 跳转到查询结果页面
     * 
     * @param model {@link ModelMap}
     * @return 查询结果页面
     */
    @RequestMapping(value = "/roleFunc.htm")
    public String quanxianList(ModelMap model, String role) {
        try {
            JSONArray re = roleFuncManager.queryQuxian(role);
            model.addAttribute("re", re.toString());
            model.addAttribute("role", role);
        } catch (Exception e) {
            log.error("查询出错：", e);
        }
        return "sys/roleFunc/roleFuncList";
    }

    /**
     * 跳转到查询结果页面
     * 
     * @param model {@link ModelMap}
     * @return 查询结果页面
     */
    @RequestMapping(value = "/saveRoleFunc.json")
    public void saveQuxanxian(ModelMap model, String role, String roleFuncIds) {
        try {
            roleFuncManager.saveQuanxian(role, roleFuncIds);
            JsonResultUtil.suncess(model, "保存成功！");
        } catch (AppException e) {
            log.warn("保存错误：", e);
            JsonResultUtil.fail(model, e.getMessage());
        }
    }

    /**
     * 刷新权限数据
     * 
     * @param model {@link ModelMap}
     * @return 查询结果页面
     */
    @RequestMapping(value = "/refRoleFunc.json")
    public void refQuxanxian(ModelMap model) {
        try {
            roleFuncManager.getSecurityData().loadAllResource();
            JsonResultUtil.suncess(model, "刷新成功！");
        } catch (Exception e) {
            log.error("刷新错误：", e);
            JsonResultUtil.fail(model, "刷新失败！");
        }
    }

}
